using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using SouthernLand.Common.BOL.NhaDat;

namespace SouthernLand.Common.DAL.NhaDat
{
    public class TinhThanhDAL : BaseDAL
    {
        #region ObjIDataReader

        /// <summary>
        /// Returns a new TinhThanh instance filled with the DataReader's current record data 
        /// </summary>
        private TinhThanh GetTinhThanhFromReader(IDataReader dr)
        {
            var obj = new TinhThanh();
            obj.TinhThanhID = (dr["TinhThanhID"] is DBNull) ? Int32.MinValue : (Int32) dr["TinhThanhID"];
            obj.TenTinhThanh = (dr["TenTinhThanh"] is DBNull) ? String.Empty : (String) dr["TenTinhThanh"];
            obj.TenTinhThanh_en = (dr["TenTinhThanh_en"] is DBNull) ? String.Empty : (String) dr["TenTinhThanh_en"];
            return obj;
        }

        #endregion

        /// <summary>
        /// Returns a collection of TinhThanh with the data read from the input DataReader
        /// </summary>
        private List<TinhThanh> GetTinhThanhCollectionFromReader(IDataReader dr)
        {
            var obj = new List<TinhThanh>();
            while (dr.Read())
                obj.Add(GetTinhThanhFromReader(dr));
            return obj;
        }

        public int Insert(TinhThanh obj)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_TinhThanh_Insert", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@TinhThanhID", SqlDbType.Int).Value = obj.TinhThanhID;
                myCommand.Parameters.Add("@TenTinhThanh", SqlDbType.NVarChar).Value = obj.TenTinhThanh;
                myCommand.Parameters.Add("@TenTinhThanh_en", SqlDbType.NVarChar).Value = obj.TenTinhThanh_en;

                return myCommand.ExecuteNonQuery();
            }
        }

        public bool Update(TinhThanh obj)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_TinhThanh_Update", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@TinhThanhID", SqlDbType.Int).Value = obj.TinhThanhID;
                myCommand.Parameters.Add("@TenTinhThanh", SqlDbType.NVarChar).Value = obj.TenTinhThanh;
                myCommand.Parameters.Add("@TenTinhThanh_en", SqlDbType.NVarChar).Value = obj.TenTinhThanh_en;

                int ret = myCommand.ExecuteNonQuery();
                return (ret == 1);
            }
        }

        public bool Delete(Int32 TinhThanhID)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_TinhThanh_Delete", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@TinhThanhID", SqlDbType.Int).Value = TinhThanhID;

                int ret = myCommand.ExecuteNonQuery();
                return (ret == 1);
            }
        }

        /// <summary>
        /// Retrieves the w4u_TinhThanh with the specified ID
        /// </summary>
        public TinhThanh GetTinhThanhByID(Int32 TinhThanhID)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_TinhThanh_GetTinhThanhByID", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@TinhThanhID", SqlDbType.Int).Value = TinhThanhID;

                SqlDataReader dr = myCommand.ExecuteReader();
                return (dr.Read() ? GetTinhThanhFromReader(dr) : null);
            }
        }

        //Get All TinhThanh
        public List<TinhThanh> GetAllTinhThanh(int PageIndex, int PageSize)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_TinhThanh_GetAllTinhThanh", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@PageIndex", SqlDbType.Int).Value = PageIndex;
                myCommand.Parameters.Add("@PageSize", SqlDbType.Int).Value = PageSize;
                SqlDataReader dr = myCommand.ExecuteReader();
                return GetTinhThanhCollectionFromReader(dr);
            }
        }

        // Get Count All records of Table  TinhThanh
        public int GetCountTinhThanh()
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_TinhThanh_GetCountTinhThanh", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                return (int) myCommand.ExecuteScalar();
            }
        }
    }
}